﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;  

public class HelpMain : MonoBehaviour {

    public GameObject m_pPlayer,m_pStair,m_pFireBall,m_pFlyBird;
    public Text m_tInfo,m_tQuestion;
    public string[] m_sNotices;
    private List<GameObject> m_lstStairs;
    private GameObject m_gPlayer;
    private bool m_bIsFireBall, m_bIsNotReborn;
    private float m_fFont, m_fTime, m_fFireBallTimer, m_fFlyBirdTimer;
    public Transform m_tStairs, m_gBack, m_gGoAhead, m_tFallBall;
    private int m_iCurrentNote;
    private PlayerScript ps;
    private Vector3 m_vBirthPoint;

	// Use this for initialization
	void Start () {
        InitialGameData();
        InitialMember();
        InitialListener();
        CommonFunctionA.Initial().initialListener();
        EventDispatcher.btnEvent += onBtnEvent;
        EventDispatcher.dropEvent += onDropEvent;
        //CommonFunctionA.InitialAudio();
	}


    private void LoadGameData()
    {
        List<StairPropertyInfo> lst = GameData.lstSPI;
        int y = (int)m_gPlayer.GetComponent<PlayerScript>().m_gCurStair.transform.position.y;
        //CommonFunctionA.logger("m_gPlayer.GetComponent<PlayerScript>().m_gCurStair.transform.position.y--->" + m_gPlayer.GetComponent<PlayerScript>().m_gCurStair.transform.position.y, 1);
        for (int i = 0; i < lst.Count; i++)
        {
            //CommonFunctionA.logger("y:" + y + "GameData.gi.iStairNumPassed:" + GameData.gi.iStairNumPassed + " result: " + y / GameData.gi.iStairNumPassed, 1);
            //CommonFunctionA.logger("lst[i].iStage" + lst[i].iStage, 1);
            if (lst[i].iStage == y / GameData.gi.iStairNumPassed)
            {
                GameData.spi = lst[i];
                GameData.gi.SGR = lst[i].sgr;
                GameData.gii = lst[i].gii;
                break;
            }
        }
    }


    void Update()
    {
        if (m_bIsNotReborn)
        {
            if (m_gPlayer.GetComponent<PlayerScript>().m_bIsDead)
            {
                //CommonFunctionA.logger("m_gPlayer.GetComponent<PlayerScript>().m_bIsDead:" + m_gPlayer.GetComponent<PlayerScript>().m_bIsDead, 1);
                m_bIsNotReborn = false;
                StartCoroutine(HelpMain_Dead(1));
            }
        }
        /*
        if (m_bIsFireBall)
        {
            m_fFireBallTime += Time.deltaTime;
            if (m_fFireBallTime > 5)
            {
                m_fFireBallTime = 0;
                Vector3 v = new Vector3((int)UnityEngine.Random.Range(-GameData.gi.fHorzMaxBound, GameData.gi.fHorzMaxBound), 10, -1);
                GameObject gFireBall; 
                int iType = 0;
                if (GameData.GetRandomRate(0.3f))
                {
                    iType = 2;
                    gFireBall = Instantiate(m_pFireBall, v, Quaternion.identity) as GameObject;
                    //CommonFunctionA.PlayAudio(gameObject, "fallball1");
                }
                else if (GameData.GetRandomRate(0.5f))
                {
                    iType = 1;
                    gFireBall = Instantiate(m_pFireBall, v, Quaternion.identity) as GameObject;
                    
                    //CommonFunctionA.PlayAudio(gameObject, "fallball2");
                }
                else
                {
                    iType = 3;
                    gFireBall = Instantiate(m_pFlyBird, v, Quaternion.identity) as GameObject;
                    
                    //CommonFunctionA.PlayAudio(gameObject, "fallball2");
                }

                gFireBall.GetComponent<FireBall>().InitialFireBall(iType, 5f);
            }
        }
        */

        if (m_iCurrentNote==5)
        {
            //GameData.logger("有火球,时间:"+m_fFireBallTimer, 1);
            if (m_fFireBallTimer <= 0)
            {
                if (m_fFireBallTimer != -1)
                {
                    float floor = m_gPlayer.GetComponent<PlayerScript>().m_gCurStair.transform.position.y;
                    Vector3 v = new Vector3((int)UnityEngine.Random.Range(-GameData.gi.fHorzMaxBound, GameData.gi.fHorzMaxBound), floor + 15, GameData.pi.fZAxisFix);
                    //测试中间掉下来
                    //Vector3 v = new Vector3(0, floor + 15, GameData.pi.fZAxisFix);
                    GameObject gFireBall = Instantiate(m_pFireBall, v, Quaternion.identity) as GameObject;
                    gFireBall.transform.parent = m_tFallBall;
                    int iType = 0;
                    if (GameData.GetRandomRate(GameData.gi.SGR.fbpFallBall.fFallBall2Rage))
                    {
                        iType = 2;
                        //CommonFunctionA.PlayAudio(gameObject, "fallball1");
                    }
                    else
                    {
                        iType = 1;
                        //CommonFunctionA.PlayAudio(gameObject, "fallball2");
                    }
                    gFireBall.GetComponent<FireBall>().InitialFireBall(iType, 5f);
                }
                m_fFireBallTimer = 5;
            }
            else
            {
                m_fFireBallTimer -= Time.deltaTime;
            }

        }

        if (m_iCurrentNote==5)
        {
            if (m_fFlyBirdTimer <= 0)
            {
                if (m_fFlyBirdTimer != -1)
                {
                    float floor = m_gPlayer.GetComponent<PlayerScript>().m_gCurStair.transform.position.y;
                    Vector3 v = new Vector3(m_gPlayer.transform.position.x - 8, floor + UnityEngine.Random.Range(-1f, 1f) * GameData.fbi.fFlyBirdRange, GameData.pi.fZAxisFix);
                    //测试中间掉下来
                    //Vector3 v = new Vector3(0, floor + 15, GameData.pi.fZAxisFix);
                    GameObject gFireBall = Instantiate(m_pFlyBird, v, Quaternion.identity) as GameObject;
                    gFireBall.transform.parent = m_tFallBall;
                    FireBall fb = gFireBall.GetComponent<FireBall>();
                    fb.m_tPlayer = m_gPlayer.transform;
                    fb.InitialFireBall(3, 10f);

                }
                m_fFlyBirdTimer = 8;
            }
            else
            {
                m_fFlyBirdTimer -= Time.deltaTime;
            }
        }
    }

    // Update is called once per frame
    private void InitialGameData()
    {
        GameData.GetInstance();
        GameData.gi.bIsGameRunning = true;
        GameData.gi.bIsGameMode = false;
        m_fFireBallTimer = -1;
        m_fFlyBirdTimer = -1;
        
    }

    private void InitialMember()
    {
        m_vBirthPoint = new Vector3(0, 0, 0);
        m_gPlayer = Instantiate(m_pPlayer, m_vBirthPoint, Quaternion.identity) as GameObject;
        m_lstStairs = new List<GameObject>();
        m_gPlayer.transform.Translate(0, GameData.pi.fYAxisFix, GameData.pi.fZAxisFix);
        m_fFireBallTimer = m_iCurrentNote = 0;
        m_bIsFireBall = false;
        m_bIsNotReborn = true;
        Initial();
    }

    private void InitialListener()
    {
        NotificationCenter.DefaultCenter().AddObserver(this, "HelpMain_Dead");
    }

    IEnumerator HelpMain_Dead(float time)
    {
        yield return new WaitForSeconds(1);        
        Destroy(m_gPlayer.GetComponent<Rigidbody2D>());
        m_gPlayer.GetComponent<PlayerScript>().m_bIsJump = false;
        Initial();
        m_bIsNotReborn = true;
        GameData.gi.bIsGameRunning = true;
        m_gPlayer.GetComponent<PlayerScript>().m_bIsFinishJump = true;
    }

    
    private void GeneralStairs()
    {
        //先删除所有的
        for (int i = 0; i < m_lstStairs.Count; i++)
        {
            Destroy(m_lstStairs[i]);
        }
        m_lstStairs.Clear();
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                Vector3 v = new Vector3(-2 + j, -2 + i, 0);
                GameObject stair = Instantiate(m_pStair, v, Quaternion.identity) as GameObject;
                stair.name = "stair_" + (int)(j + i * 5);
                m_lstStairs.Add(stair);
                stair.transform.parent = m_tStairs;
                stair.GetComponent<StairProperty>().m_bIsLive = true;
                if (i == 2 && j == 2)
                {
                    m_gPlayer.transform.position = v;
                    m_gPlayer.transform.Translate(0, GameData.pi.fYAxisFix, GameData.pi.fZAxisFix);
                    m_gPlayer.GetComponent<PlayerScript>().m_gCurStair = stair;
                }
                stair.GetComponent<StairProperty>().InitialStair(1, 0);
            }
        }
    }
    
    private void Initial()
    {
        m_gPlayer.transform.position = m_vBirthPoint;
        m_gPlayer.GetComponent<PlayerScript>().m_bIsDead = false;
        m_gPlayer.GetComponent<SpriteRenderer>().color = Color.white;
        GeneralStairs();
        m_bIsFireBall = false;
        m_tInfo.text = m_sNotices[m_iCurrentNote];
        Camera.main.transform.position = new Vector3(0, GameData.ci.fYAxisFix, GameData.ci.fZAxisFix);
        if (m_iCurrentNote == 0)
        {
            //将退后失效
            m_gBack.gameObject.SetActive(false);
        }
        else if (m_iCurrentNote == m_sNotices.Length - 1)
        {
            //将前进失效
            m_gGoAhead.gameObject.SetActive(false);
        }
        else
        {
            m_gBack.gameObject.SetActive(true);
            m_gGoAhead.gameObject.SetActive(true);
        }
        switch (m_iCurrentNote)
        {
            case 0:
                
                break;
            case 1:
                //将一些设置为树叶
                m_lstStairs[16].GetComponent<StairProperty>().InitialStair(2, 3);
                m_lstStairs[23].GetComponent<StairProperty>().InitialStair(2, 3);
                break;
            case 2:
                //将一些设置为冰块
                m_lstStairs[16].GetComponent<StairProperty>().InitialStair(2, 1);
                m_lstStairs[23].GetComponent<StairProperty>().InitialStair(2, 1);
                break;

            case 3:
                //将一些设置为火焰
                m_lstStairs[11].GetComponent<StairProperty>().InitialStair(3, 3);
                m_lstStairs[11].GetComponent<StairProperty>().enableEffect();
                m_lstStairs[18].GetComponent<StairProperty>().InitialStair(3, 1.2f);
                m_lstStairs[18].GetComponent<StairProperty>().enableEffect();
                break;

            case 4:
                //将一些设置为问题
                m_lstStairs[17].GetComponent<StairProperty>().InitialStair(4, 0, new MsgBox("［数学题］", GameData.qi.LstQuestions[1].sQuestion, "关闭", GameData.gi.fMsgBoxAutoCloseSec));
                m_lstStairs[17].transform.GetChild(0).GetChild(2).GetComponent<Text>().text = "";
                //m_lstStairs[17].transform.GetChild(0).GetChild(1).GetComponent<Text>().fontSize = 25;
                m_lstStairs[17].transform.GetChild(0).GetChild(2).gameObject.SetActive(true);
                m_lstStairs[17].transform.GetChild(0).GetChild(2).Translate(0, -0.2f, 0);
                MsgBox Mb=new MsgBox();
                m_lstStairs[21].GetComponent<StairProperty>().InitialStair(5, 0, Mb, 1, 1);
                m_lstStairs[22].GetComponent<StairProperty>().InitialStair(5, 0, Mb, 1, 2);
                m_lstStairs[22].GetComponent<StairProperty>().m_bIsLive = false;
                m_lstStairs[23].GetComponent<StairProperty>().InitialStair(5, 0, Mb, 1, 3);
                m_lstStairs[23].GetComponent<StairProperty>().m_bIsLive = false;
                m_tInfo.text = m_tInfo.text + "例如" + GameData.qi.LstQuestions[1].sQuestion;
                m_lstStairs[15].gameObject.SetActive(false);
                m_lstStairs[16].gameObject.SetActive(false);
                m_lstStairs[18].gameObject.SetActive(false);
                m_lstStairs[19].gameObject.SetActive(false);
                break;

            case 5:
                m_bIsFireBall = true;
                break;
        }
        LoadGameData();
    }

    public void onBtnEvent(GameObject btn, UserOperate.btnState btnState)
    {
        if (btnState == UserOperate.btnState.btnStateUp && btn.name == "Help_btnBack")
        {
            //退出上一阶段
            if (m_iCurrentNote > 0)
            {
                m_iCurrentNote--;
                Initial();
            }
        }
        else if (btnState == UserOperate.btnState.btnStateUp && btn.name == "Help_btnGoAhead")
        {
            //退出上一阶段
            if (m_iCurrentNote < 5)
            {
                m_iCurrentNote++;
                Initial();
            }
        }

        else if (btnState == UserOperate.btnState.btnStateUp && btn.name == "Help_btnRefresh")
        {
            //退出上一阶段
            Initial();
        }

        else if (btnState == UserOperate.btnState.btnStateUp && btn.name == "Help_btnHome")
        {
            BackHomePage();
        }
    }

    public void onDropEvent(GameObject obj, UserOperate.dragState btnState, PointerEventData eventData)
    {
        
    }

    private void BackHomePage()
    {
        SceneManager.LoadScene("Start");
    }
     
}
